
package com.wyzz.hr.action;

import java.util.*;

import com.wyzz.hr.base.BaseAction;
import com.wyzz.hr.base.ComonBeans;
import com.wyzz.hr.base.DWRUtil;
import com.wyzz.hr.bo.IRecruitappcontactBo;
import com.wyzz.hr.bo.IRecruitapplierBo;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.Recruitappcontact;
import com.wyzz.hr.pojo.Recruitapplier;

public class RecruitAppcontactManage extends BaseAction {

	public RecruitAppcontactManage() {
		appcontactBo = (IRecruitappcontactBo) getBean("appcontactBo");
		applierBo = (IRecruitapplierBo) getBean("applierBo");
	}

	public String execute() throws Exception {
		reccontactList = appcontactBo.getContactListByApp(appId, "0");
		catNo = "0";
		initParams();
		return "success";
	}

	public String searchContactHR() throws Exception {
		reccontactList = appcontactBo.getContactListByApp(appId, "1");
		catNo = "1";
		initParams();
		return "success";
	}

	private void initParams() {
		recacEmpName = getCurrentEmp().getEmpName();
		recaEmpContactList = ComonBeans.getValuesToMap("recaEmpContact",
				new boolean[] { false });
		recaHRContactList = ComonBeans.getValuesToMap("recaHRContact",
				new boolean[] { false });
		recaEmpRateList = ComonBeans.getValuesToMap("recaEmpRate",
				new boolean[] { false });
		recaHRRateList = ComonBeans.getValuesToMap("recaHRRate",
				new boolean[] { false });
		recApplier = applierBo.loadApplier(appId,
				new String[] { Recruitapplier.PROP_RECA_INTERVIEWER });
	}

	private Integer getApplierStatus(String appId) {
		Recruitapplier applier = applierBo.loadApplierById(appId);
		Integer status = applier.getRecaStatus();
		return status;
	}

	public boolean checkInterviewer(Recruitappcontact contact) {
		Recruitapplier applier = applierBo.loadApplierById(contact
				.getRecacRecaId().getId());
		if (applier.getRecaPlanId() == null
				|| applier.getRecaPlanId().getRecpInterviewEmps() == null)
			return false;
		String emps[] = applier.getRecaPlanId().getRecpInterviewEmps().split(
				";");
		String arr$[] = emps;
		int len$ = arr$.length;
		for (int i$ = 0; i$ < len$; i$++) {
			String e = arr$[i$];
			if (getCurrentEmpNo().equals(e.trim()))
				return true;
		}

		return false;
	}

	public Recruitappcontact addAppcontact(Recruitappcontact contact) {
		String auth = DWRUtil.checkAuth("appcontactManage", "execute");
		if ("error".equalsIgnoreCase(auth)) {
			contact.setInfo("NOAUTH");
			return contact;
		}
		if (contact.getRecacCatNo().intValue() == 0
				&& !checkApplierStatus(contact.getRecacRecaId().getId())
				&& !checkInterviewer(contact)) {
			contact.setInfo("NOOPER");
			return contact;
		}
		Employee currentEmp = getCurrentEmp();
		contact.setRecacCreateBy(currentEmp);
		contact.setRecacCreateTime(new Date());
		contact.setRecacLastChangeBy(currentEmp);
		contact.setRecacLastChangeTime(new Date());
		Recruitapplier applier = applierBo.loadApplier(contact.getRecacRecaId()
				.getId(), null);
		contact.setRecacRecaId(applier);
		contact.setRecacRecpId(applier.getRecaPlanId());
		boolean result = appcontactBo.addAppcontact(contact, getCurrentEmp());
		if (result) {
			contact.setInfo("SUCC");
		} else {
			contact.setInfo("FAIL");
			return contact;
		}
		contact = appcontactBo.loadAppContact(contact.getId(), new String[] {
				Recruitappcontact.PROP_RECAC_RECA_ID,
				(new StringBuilder()).append(
						Recruitappcontact.PROP_RECAC_RECA_ID).append(".")
						.append(Recruitapplier.PROP_RECA_INTERVIEWER)
						.toString() });
		if (contact.getRecacRecaId() != null
				&& contact.getRecacRecaId().getRecaInterviewer() != null) {
			contact.setInterviewerName(contact.getRecacRecaId()
					.getRecaInterviewer().getEmpName());
			contact.setInterviewerId(contact.getRecacRecaId()
					.getRecaInterviewer().getId());
		}
		contact.setInterviewTime(contact.getRecacRecaId()
				.getRecaInterviewTime());
		return contact;
	}

	public Recruitappcontact updateAppcontact(Recruitappcontact contact) {
		String auth = DWRUtil.checkAuth("appcontactManage", "execute");
		if ("error".equalsIgnoreCase(auth)) {
			contact.setInfo("NOAUTH");
			return contact;
		}
		if (contact.getRecacCatNo().intValue() == 0
				&& !checkApplierStatus(contact.getRecacRecaId().getId())
				&& !checkInterviewer(contact)) {
			contact.setInfo("NOOPER");
			return contact;
		}
		Recruitappcontact contactInDB = appcontactBo.loadAppContact(contact
				.getId(), new String[] {
				Recruitappcontact.PROP_RECAC_RECA_ID,
				(new StringBuilder()).append(
						Recruitappcontact.PROP_RECAC_RECA_ID).append(".")
						.append(Recruitapplier.PROP_RECA_INTERVIEWER)
						.toString() });
		contactInDB.setRecacEmpName(contact.getRecacEmpName());
		contactInDB.setRecacType(contact.getRecacType());
		contactInDB.setRecacTime(contact.getRecacTime());
		contactInDB.setRecacContent(contact.getRecacContent());
		contactInDB.setRecacRate(contact.getRecacRate());
		contactInDB.setInterviewerName(contact.getInterviewerName());
		contactInDB.setInterviewerId(contact.getInterviewerId());
		contactInDB.setInterviewTime(contact.getInterviewTime());
		Employee currentEmp = getCurrentEmp();
		contactInDB.setRecacLastChangeBy(currentEmp);
		contactInDB.setRecacLastChangeTime(new Date());
		boolean result = appcontactBo.updateAppcontact(contactInDB,
				getCurrentEmp());
		if (result)
			contact.setInfo("SUCC");
		else
			contact.setInfo("FAIL");
		return contactInDB;
	}

	public Recruitappcontact selcetLastContact(String applierId) {
		String auth = DWRUtil.checkAuth("appcontactManage", "execute");
		if ("error".equalsIgnoreCase(auth))
			return null;
		IRecruitappcontactBo appcontactBo = (IRecruitappcontactBo) getBean("appcontactBo");
		List contactList = appcontactBo.getContactListByApp(applierId, "0");
		if (contactList != null && contactList.size() > 0) {
			Recruitappcontact rapp = (Recruitappcontact) contactList.get(0);
			if (rapp.getRecacCreateBy().equals(getCurrentEmp()))
				return rapp;
		}
		return null;
	}

	public String deleteAppcontact(String id) {
		String auth = DWRUtil.checkAuth("appcontactManage", "execute");
		if ("error".equalsIgnoreCase(auth))
			return "NOAUTH";
		if (appcontactBo.deleteAppcontact(id))
			return "SUCC";
		else
			return "FAIL";
	}

	public boolean checkApplierStatus(String appId) {
		Integer status = getApplierStatus(appId);
		return status.intValue() == 11;
	}

	public String getEmpRateStr(String index) {
		return ComonBeans.getParameterValue("recaEmpRate",
				new String[] { index });
	}

	public String getHRRateStr(String index) {
		return ComonBeans.getParameterValue("recaHRRate",
				new String[] { index });
	}

	public String getAppId() {
		return appId;
	}

	public void setAppId(String appId) {
		this.appId = appId;
	}

	public String getCatNo() {
		return catNo;
	}

	public void setCatNo(String catNo) {
		this.catNo = catNo;
	}

	public List getReccontactList() {
		return reccontactList;
	}

	public void setReccontactList(List reccontactList) {
		this.reccontactList = reccontactList;
	}

	public String getRecacEmpName() {
		return recacEmpName;
	}

	public void setRecacEmpName(String recacEmpName) {
		this.recacEmpName = recacEmpName;
	}

	public Map getRecaEmpContactList() {
		return recaEmpContactList;
	}

	public void setRecaEmpContactList(Map recaEmpContactList) {
		this.recaEmpContactList = recaEmpContactList;
	}

	public Map getRecaHRContactList() {
		return recaHRContactList;
	}

	public void setRecaHRContactList(Map recaHRContactList) {
		this.recaHRContactList = recaHRContactList;
	}

	public Map getRecaEmpRateList() {
		return recaEmpRateList;
	}

	public void setRecaEmpRateList(Map recaEmpRateList) {
		this.recaEmpRateList = recaEmpRateList;
	}

	public Map getRecaHRRateList() {
		return recaHRRateList;
	}

	public void setRecaHRRateList(Map recaHRRateList) {
		this.recaHRRateList = recaHRRateList;
	}

	public Integer getRecaStatus() {
		return recaStatus;
	}

	public void setRecaStatus(Integer recaStatus) {
		this.recaStatus = recaStatus;
	}

	public Recruitapplier getRecApplier() {
		return recApplier;
	}

	public void setRecApplier(Recruitapplier recApplier) {
		this.recApplier = recApplier;
	}

	private String appId;
	private String catNo;
	private String recacEmpName;
	private List reccontactList;
	private Map recaEmpContactList;
	private Map recaHRContactList;
	private Map recaEmpRateList;
	private Map recaHRRateList;
	private Integer recaStatus;
	private Recruitapplier recApplier;
	private static final String NOAUTH = "NOAUTH";
	private static final String SUCC = "SUCC";
	private static final String FAIL = "FAIL";
	private static final String NOOPER = "NOOPER";
	private IRecruitappcontactBo appcontactBo;
	private IRecruitapplierBo applierBo;
}
